package com.itic.ht.cms.question.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.basevo.User;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.cms.question.persistence.dao.QuestionModelDao;
import com.itic.ht.cms.question.persistence.model.QuestionModel;
import com.itic.ht.cms.question.persistence.model.QuestionModelExample;


@Service
@Transactional
public class QuestionModelService extends CrudService<QuestionModelDao,QuestionModel,QuestionModelExample> {
    @Autowired
    private QuestionModelDao questionModelDao;

    //批量导入业务
	public String imp(HttpServletRequest request,List<QuestionModel> dataList) {
		if(dataList.size()>0){
			Date date = new Date();
			User user = (User)request.getSession().getAttribute(IticConst.PS_SESSION_USERINFO_KEY);
			
			for(QuestionModel temp:dataList){
				temp.setId(IdGen.uuid());
				temp.setCreateTime(date);
				temp.setUpdateTime(date);
				temp.setIsEffective(DBConst.TRUE);
				
				temp.setOrgCode(user.getOrg());
				temp.setCreateUser(user.getId());
				questionModelDao.insert(temp);
			}
		}
		return "success";
	}
	//批量导入业务
	public String addTemplet(QuestionModel temp) {
        try {
        	questionModelDao.insert(temp);
        	return "success";
		} catch (Exception e) {
			return "error";
		}
	}
	public List<QuestionModel> getQuestionList(String category) {
		QuestionModelExample example = new QuestionModelExample();
		example.setOrderByClause("reorder asc");
		example.createCriteria().andCategoryIdEqualTo(category).andIsEffectiveEqualTo(DBConst.TRUE);
		return questionModelDao.selectByExample(example);
	}
	public List<QuestionModel> getExamQuestionList(int progress, int questionNum, String categoryId,String isRandom) {
		Map<String,Object> map = new HashMap<>();
		if( StringHelper.isNotBlank(isRandom) ){
			List<QuestionModel> resultList = new ArrayList<QuestionModel>();
			//获得总条数
			QuestionModelExample example = new QuestionModelExample();
			example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andCategoryIdEqualTo(categoryId);
			int total = questionModelDao.countByExample(example);
			if( questionNum>0 ){
				for( int i=0;i<questionNum;i++ ){
					Random random = new Random();
					map.put("progress", random.nextInt(total));
					map.put("questionNum", 1);
					map.put("categoryId", categoryId);
					List<QuestionModel> examQuestionList = questionModelDao.getExamQuestionList(map);
					resultList.addAll(examQuestionList);
				}
			}
			return resultList;
		}else{
			map.put("progress", progress);
			map.put("questionNum", questionNum);
			map.put("categoryId", categoryId);
			return questionModelDao.getExamQuestionList(map);
		}
	}


}